#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <pthread.h>
#include <errno.h>
#include <time.h>

#define SERVPORT 9999
#define REMOTE_IP "192.168.101.54"

int main(int argc, char *argv[])
{
	int sockfd;

	struct sockaddr_in serv_addr;

	if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
	{
		perror("socket error!");
		return -1;
	}

	serv_addr.sin_family = AF_INET;
	serv_addr.sin_addr.s_addr = inet_addr(REMOTE_IP);
	serv_addr.sin_port = htons(SERVPORT);
	bzero(&(serv_addr.sin_zero),8);

	while(-1 == connect(sockfd,(struct sockaddr*)&serv_addr,sizeof(struct sockaddr)))
	{
		perror("connect error");
	}

	struct sockaddr_in serv_peer;
	socklen_t t_len = sizeof(struct sockaddr);
	getpeername(sockfd, (struct sockaddr*)&serv_peer, &t_len);
	printf("remote ip = [%s]\n", inet_ntoa(serv_peer.sin_addr));

	char buf[2048] = {0};
	while(1)
	{
		memset(buf, 0, 2048);
		memset(buf, 'f', 2047);

		if(0 >= send(sockfd, &buf, 2048, 0))
		{
			printf("send error");
			getpeername(sockfd, (struct sockaddr*)&serv_peer, &t_len);
			printf("remote ip = [%s]\n", inet_ntoa(serv_peer.sin_addr));
			return -1;
		}
		sleep(3);
	}
	close(sockfd);
	return 0;
}
